package org.foundation.jala.jamerzip.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import org.foundation.jala.jamerzip.modelzip.ZipManager;

public class MainView extends JFrame {
	private static final long serialVersionUID = 1L;
	private ZipManager actions;
	private JTabbedPane tabbed;

	public MainView() {
		super("JAM Zip");
		configuration();
		initComponents();
		setVisible(true);
	}

	public void setActions(ZipManager actions) {
		this.actions = actions;
	}

	private void configuration() {
		this.setSize(650, 500);
		this.setMinimumSize(new Dimension(650, 500));
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private JButton createButtonWithImageAndToolTip(String path, String toolTip) {
		JButton button = new JButton(new ImageIcon(path));
		button.setToolTipText(toolTip);
		return button;
	}
	public int getSelectTab(){
		return this.tabbed.getSelectedIndex();
	}

	@SuppressWarnings("serial")
	private void initComponents() {
		/** Adding menu bar **/
		ZipMenuBar menuBar = new ZipMenuBar();
		this.setJMenuBar(menuBar);

		/** Adding mainly buttons **/

		JPanel pannelOfButtons = new JPanel();
		pannelOfButtons.setMaximumSize(new Dimension(600, 100));
		pannelOfButtons.setPreferredSize(new Dimension(600, 100));
		pannelOfButtons.setLayout(new BoxLayout(pannelOfButtons,
				BoxLayout.X_AXIS));
		pannelOfButtons.add(Box.createHorizontalStrut(40));

		JButton newZip = createButtonWithImageAndToolTip("resource/i.png",
				"Compress one or more files ");
		AbstractAction create = new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actions.createNewZip();
				Logger.getLogger("Action").log(Level.INFO, "NEW ZIP");
			}
		};
		newZip.addActionListener(create);
		menuBar.addActionToItem(0, 0, create);

		pannelOfButtons.add(newZip);
		pannelOfButtons.add(Box.createHorizontalStrut(15));

		JButton open = createButtonWithImageAndToolTip("resource/oo.png",
				"Open a zip file");
		AbstractAction openZip = new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actions.openZip();
				Logger.getLogger("Action").log(Level.INFO, "OPEN ZIP");	
			}
		};
		open.addActionListener(openZip);
		menuBar.addActionToItem(0, 2, openZip);
		pannelOfButtons.add(open);
		pannelOfButtons.add(Box.createHorizontalStrut(15));
		menuBar.addActionToItem(0, 3, new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		JButton extract = createButtonWithImageAndToolTip("resource/e.png",
				"Extract content");
		AbstractAction extractAction = new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actions.extractContent();
				Logger.getLogger("Action").log(Level.INFO, "EXTRACT CONTENT");
			}
		};
		extract.addActionListener(extractAction);
		menuBar.addActionToItem(1, 1, extractAction);
		pannelOfButtons.add(extract);
		pannelOfButtons.add(Box.createHorizontalStrut(15));

		JButton properties = createButtonWithImageAndToolTip("resource/p.png",
				"Show advanced properties");
		AbstractAction show = new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actions.showProperties();
				Logger.getLogger("Action").log(Level.INFO, "SHOW PROPERTIES");
			}
		};
		properties.addActionListener(show) ;
		menuBar.addActionToItem(1, 0, show);
		pannelOfButtons.add(properties);
		pannelOfButtons.add(Box.createHorizontalStrut(15));

		JPanel panelViewer = new JPanel();
		panelViewer.setLayout(new BorderLayout());
		panelViewer.setMinimumSize(new Dimension(700, 700));

		/** Creating buttons to navigate **/

		JPanel pannelBtNavigate = new JPanel();
		pannelBtNavigate.setMaximumSize(new Dimension(300, 60));
		pannelBtNavigate.setPreferredSize(new Dimension(300, 60));
		pannelBtNavigate.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		pannelBtNavigate.setLayout(new BoxLayout(pannelBtNavigate,
				BoxLayout.X_AXIS));
		pannelBtNavigate.add(Box.createHorizontalStrut(20));

		JButton back = createButtonWithImageAndToolTip("resource/back.png",
				"Navigate at previous folder");
		pannelBtNavigate.add(back);
		JButton root = createButtonWithImageAndToolTip("resource/home.png",
				"Go to root directory");
		pannelBtNavigate.add(root);
		JButton forward = createButtonWithImageAndToolTip(
				"resource/forward.png", "Navigate at next folder");
		pannelBtNavigate.add(forward);
		pannelBtNavigate.add(Box.createHorizontalStrut(50));
		JButton search = new JButton("Search");
		pannelBtNavigate.add(search);
		pannelBtNavigate.add(Box.createHorizontalStrut(10));
		JTextField pathSearch = new JTextField("  ");
		pathSearch.setMaximumSize(new Dimension(500, 30));
		pathSearch.setPreferredSize(new Dimension(500, 30));
		pathSearch.setMaximumSize(new Dimension(500, 30));
		pannelBtNavigate.add(pathSearch);

		panelViewer.add(pannelBtNavigate, BorderLayout.NORTH);

		this.tabbed = new JTabbedPane(JTabbedPane.TOP,
				JTabbedPane.SCROLL_TAB_LAYOUT);
		this.tabbed.setAutoscrolls(true);
		panelViewer.add(tabbed, BorderLayout.CENTER);
		this.add(pannelOfButtons, BorderLayout.NORTH);
		this.add(panelViewer, BorderLayout.CENTER);
	}

	public void addToTabbed(String name, JPanel panel) {
		String tabName = name.substring(name.lastIndexOf('/') + 1,
				name.length());
		this.tabbed.addTab(tabName, panel);
		addCloseButton(tabName,name);
	}

	@SuppressWarnings("serial")
	private void addCloseButton(String title,String tooltip) {
		int index = this.tabbed.indexOfTab(title);
		JPanel pnlTab = new JPanel(new GridBagLayout());
		pnlTab.setOpaque(false);
		JLabel lblTitle = new JLabel(title);
		JButton btnClose = new JButton("x");
		lblTitle.setToolTipText(tooltip);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.BOTH;
		pnlTab.add(lblTitle, gbc);

		gbc.gridx++;
		gbc.weightx = 0;
		pnlTab.add(btnClose, gbc);

		this.tabbed.setTabComponentAt(index, pnlTab);

		btnClose.addActionListener(new AbstractAction() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int index = tabbed.getSelectedIndex();
				tabbed.remove(index);
				actions.remove(index);
			}
		});
	}
	
	
}